#!/user/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Sjy
Description: 数据库模块
"""
from typing import Dict, Any

from .base import BaseModule
import gui.controls.envs as envs
from ..params_model import DlgCompositePly, DlgAdhesiveLayer, DlgHoneycombCore
from ..project import project_manager
import json


class DatabaseModule(BaseModule):
    """数据库模块"""

    def __init__(self):
        super().__init__()

    def init_category(self, ribbon: envs.Ribbonbar) -> Any:
        """初始化工具栏项
        Args:
            ribbon: 工具栏
        Returns:
            Any: 工具栏项
        """
        categoryDatabase = ribbon.add_category("数据库")
        pannelMaterials = ribbon.add_panel("材料库", categoryDatabase, titleHide=True)

        ribbon.add_large_btn(
            pannelMaterials,
            {
                "title": "层合板",
                "style": "large",
                "styleSheet": "margin-right:10px; padding: 10px 20px;",
                "action": lambda *args: self.show_dlg(flag="compositePly"),
            },
        )
        ribbon.add_large_btn(
            pannelMaterials,
            {
                "title": "蜂窝",
                "style": "large",
                "styleSheet": "margin-right:10px; padding: 10px 20px;",
                "action": lambda *args: self.show_dlg(flag="honeycombCore"),
            },
        )

        ribbon.add_large_btn(
            pannelMaterials,
            {
                "title": "胶层",
                "style": "large",
                "styleSheet": "margin-right:10px; padding: 10px 20px;",
                "action": lambda *args: self.show_dlg(flag="adhesiveLayer"),
            },
        )

        ribbon.add_large_btn(
            pannelMaterials,
            {
                "title": "金属",
                "style": "large",
                "styleSheet": "margin-right:10px; padding: 10px 20px;",
                "action": lambda *args: self.show_dlg(flag="metal"),  # 暂时没有
            },
        )
        return categoryDatabase

    def init_dock(self, tree) -> None:
        """更新参数库树"""
        data = [
            {
                "title": "数据库",
                "children": [
                    {
                        "title": "层合板",
                        "events": {
                            "dbClick": lambda params: self.show_dlg(flag="compositePly")
                        },
                    },
                    {
                        "title": "蜂窝",
                        "events": {
                            "dbClick": lambda params: self.show_dlg(
                                flag="honeycombCore"
                            )
                        },
                    },
                    {
                        "title": "胶层",
                        "events": {
                            "dbClick": lambda params: self.show_dlg(
                                flag="adhesiveLayer"
                            )
                        },
                    },
                    {
                        "title": "金属",
                        "events": {
                            "dbClick": lambda params: self.show_dlg(
                                flag="metal"
                            )  # 暂时没有
                        },
                    },
                ],
            }
        ]
        tree._set_data(data)

    def show_dlg(self, flag: str):
        """显示对话框"""
        py3d = envs.GL.get_value("py3d")
        dlg = None
        if flag == "compositePly":
            dlg = DlgCompositePly.show(py3d)
            if dlg:
                dlg.init(project_manager.get_database_data_by_type("Composite"))
                dlg.compositePly_save_signal.connect(
                    lambda data: self.compositePly_save_signal(data)
                )
        elif flag == "adhesiveLayer":
            dlg = DlgAdhesiveLayer.show(py3d)
            if dlg:
                dlg.init(project_manager.get_database_data_by_type("AdhesiveLayer"))
                dlg.adhesiveLayer_save_signal.connect(
                    lambda data: self.adhesiveLayer_save_signal(data)
                )
        elif flag == "honeycombCore":
            dlg = DlgHoneycombCore.show(py3d)
            if dlg:
                dlg.init(project_manager.get_database_data_by_type("Honeycomb"))
                dlg.honeycombCore_save_signal.connect(
                    lambda data: self.honeycombCore_save_signal(data)
                )
        elif flag == "metal":
            envs.MBox.warning("暂未提供金属材料数据表结构")
            # dlg = DlgHoneycombCore.show(py3d)
            # if dlg:
            #     dlg.init(project_manager.get_database_data_by_type("Metal"))
            #     dlg.honeycombCore_save_signal.connect(lambda data: self.honeycombCore_save_signal(data)) # 暂时没有

        cfgIndex = {}
        with open("config/sys/index.json", "r", encoding="utf-8") as file:
            cfgIndex = json.load(file) or {}
        theme = cfgIndex.get("theme", "dark")  # dark or light
        qss = "styles/dlg.qss"
        if theme == "light":
            qss = "styles/dlg_light.qss"

        dlg and dlg.setStyleSheet(open(qss, "r").read())

    def compositePly_save_signal(self, data: dict = {}) -> None:
        """层合板"""
        project_manager.set_database_data_by_type("Composite", data)

    def adhesiveLayer_save_signal(self, data: dict = {}) -> None:
        """胶层"""
        project_manager.set_database_data_by_type("AdhesiveLayer", data)

    def honeycombCore_save_signal(self, data: dict = {}) -> None:
        """蜂窝"""
        project_manager.set_database_data_by_type("Honeycomb", data)

    def metal_save_signal(self, data: dict = {}) -> None:
        """金属"""
        project_manager.set_database_data_by_type("Metal", data)
